﻿using LinqKit;
using Microsoft.Extensions.Configuration;
using OConf.Entity;
using System.Collections.Generic;
using System.Linq;

namespace OConf.Service
{
    public class ConfigInfoService
    {
        private IConfiguration _configuration { get; }
        public ConfigInfoService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public bool IsExistsByKey(string configKey, string projectId, string nameSpaceID)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.ConfigInfo.Any(w => w.ConfigKey == configKey && w.ProjectID == projectId && w.NameSpaceID == nameSpaceID);
            }
            return isExists;
        }

        /// <summary>
        /// 命名空间下配置信息是否全部发布
        /// </summary>
        /// <param name="nameSpaceID"></param>
        /// <returns></returns>
        public bool IsPublishAll(string nameSpaceID)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.ConfigInfo.Any(w => w.NameSpaceID == nameSpaceID && w.State != (int)ConfigInfoState.已发布);
            }
            return !isExists;
        }

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddEntity(ConfigInfoEntity entity)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.ConfigInfo.Add(entity);
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string id)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.ConfigInfo.Remove(new ConfigInfoEntity() { ID = id });
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 修改配置信息状态
        /// </summary>
        /// <param name="key"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateState(string key, ConfigInfoState state)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ConfigInfoEntity entity = new ConfigInfoEntity()
                {
                    ID = key,
                    State = (int)state
                };
                db.ConfigInfo.Attach(entity);
                db.ConfigInfo.Entry(entity).Property(p => p.State).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 修改基础信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(ConfigInfoEntity entity)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.ConfigInfo.Attach(entity);
                db.Entry(entity).Property(p => p.ConfigValue).IsModified = true;
                db.Entry(entity).Property(p => p.ConfigDescription).IsModified = true;
                db.Entry(entity).Property(p => p.State).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 修改描述
        /// </summary>
        /// <param name="id"></param>
        /// <param name="configDescription"></param>
        /// <returns></returns>
        public bool UpdateDescription(string id, string configDescription)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ConfigInfoEntity entity = new ConfigInfoEntity()
                {
                    ID = id,
                    ConfigDescription = configDescription
                };

                db.ConfigInfo.Attach(entity);
                db.Entry(entity).Property(p => p.ConfigDescription).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        public ConfigInfoEntity FindEntity(string key)
        {
            ConfigInfoEntity entity = null;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                entity = db.ConfigInfo.Where(w => w.ID == key).FirstOrDefault();
            }
            return entity;
        }

        /// <summary>
        /// 根据NameSpaceID获取相关配置信息
        /// </summary>
        /// <param name="nameSpaceID"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public List<ConfigInfoEntity> GetConfigInfoByNameSpaceID(string nameSpaceID, ConfigInfoState? state = null)
        {
            List<ConfigInfoEntity> result = new List<ConfigInfoEntity>();

            var where = PredicateBuilder.New<ConfigInfoEntity>(true);
            where = where.And(w => w.NameSpaceID == nameSpaceID);
            if (state.HasValue)
            {
                where = where.And(w => w.State == (int)state);
            }

            using (MyDBContext db = new MyDBContext(_configuration))
            {
                result = db.ConfigInfo.Where(where)
                    .OrderBy(w => w.CreateTime).ToList();
            }
            return result;
        }
    }
}
